﻿@using Newtouch.Infrastructure;
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Index.cshtml";

    //开关：是否开放检验检查
    var IsOpenJyJcSwitch = ViewBag.IsOpenJyJcSwitch as bool?;
    //开关：门诊是否开放康复处方
    var isOpenKfcf = (ViewBag.ISOpenKfcf as bool?);
    //开关：门诊是否开放常规项目处方
    var isOpenCgxmcf = (ViewBag.ISOpenCgxmcf as bool?);

    var iSOpenZycf = (ViewBag.ISOpenZycf as bool?);
    //开关：预约挂号
    var isOpenBespeakRegister = (ViewBag.ISOpenBespeakRegister as bool?);
}

@*导航栏*@
<link href="@SiteUrl.GetStaticResourceScriptUrl("~/Content/css/cispage.css", false)" rel="stylesheet" />

<script src="@SiteUrl.GetStaticResourceScriptUrl("~/js/layout/jquery.layout.js")"></script>
<script src="@SiteUrl.GetStaticResourceScriptUrl("~/js/wdtree/tree.js")"></script>
<link href="@SiteUrl.GetStaticResourceScriptUrl("~/js/wdtree/tree.css")" rel="stylesheet" />
<link href="@SiteUrl.GetStaticResourceScriptUrl("~/css/framework-ui-compact.css")" rel="stylesheet" />
<script src="@SiteUrl.GetStaticResourceScriptUrl("~/js/validate/jquery.validate.min.js")"></script>

<ul class="nav nav-tabs navb-bg" id="myTab" style="position:fixed;top:0;z-index:100009;width:99%">
    <li role="presentation" id="librlb"><a href="#linkbrlb" role="tab" data-toggle="tab">患者列表</a></li>
    <li role="presentation"><a href="#linkbl" role="tab" data-toggle="tab">病历</a></li>
    @if (isOpenKfcf == true)
    {
        <li role="presentation"><a href="#linkkfcf" role="tab" data-toggle="tab">康复处方</a></li>
    }
    @if (isOpenCgxmcf == true)
    {
        <li role="presentation"><a href="#linkcgxmcf" role="tab" data-toggle="tab">常规项目</a></li>
    }
    <li role="presentation"><a href="#linkxycf" role="tab" data-toggle="tab">西药处方</a></li>
    @if (iSOpenZycf == true)
    {
        <li role="presentation"><a href="#linkzycf" role="tab" data-toggle="tab">中药处方</a></li>
    }

    @if (IsOpenJyJcSwitch == true)
    {
        <li role="presentation" id="lijy"><a href="#linkjy" role="tab" data-toggle="tab">检验</a></li>
        <li role="presentation" id="lijc"><a href="#linkjc" role="tab" data-toggle="tab">检查</a></li>
    }
    @if (isOpenBespeakRegister == true)
    {
        <li role="presentation" id="libespeakRegister"><a href="#linkbespeakRegister" role="tab" data-toggle="tab">预约</a></li>
    }
</ul>

<div id="divPatientBasicInfo" style="position: fixed;top: 0px;z-index: 100009;width: 31%;line-height: 40px;font-size: 18px;white-space: nowrap;text-overflow: ellipsis;background: #e9f6fe;right: 1.2%;text-align:right;padding-right: 18px;display:none;color:#07bbf1;">
</div>

<div style="margin-top:40px;">
    @Html.Partial("_LeftMostPartDiv")
    @Html.Partial("_PresTemplateTreeForm")
    @Html.Partial("_HistoryPresTreeForm")
    @Html.Partial("_InspectTemplateTreeForm")
    <div class="tab-content">
        @Html.Partial("~/Views/PatientList/PatientList.cshtml")
        @Html.Partial("~/Views/MedicalRecord/MedicalRecord.cshtml")
        @if (isOpenKfcf == true)
        {
            @Html.Partial("~/Views/Prescription/RehabPrescription.cshtml")
        }
        @if (isOpenCgxmcf == true)
        {
            @Html.Partial("~/Views/Prescription/RegularItemPrescription.cshtml")
        }
        @Html.Partial("~/Views/Prescription/WMPrescription.cshtml")
        @if (iSOpenZycf == true)
        {
            @Html.Partial("~/Views/Prescription/TCMPrescription.cshtml")
        }

        @if (IsOpenJyJcSwitch == true)
        {
            @Html.Partial("~/Views/Apply/Inspection.cshtml")
            @Html.Partial("~/Views/Apply/Examination.cshtml")
        }
        @if (isOpenBespeakRegister == true)
        {
            @Html.Partial("~/Views/BespeakRegister/BespeakRegister.cshtml")
        }
    </div>
</div>

<script>
    //开关：是否开放生命体征
    var IsVitalSignsSwitch = '@ViewBag.IsVitalSignsSwitch';
    //登录人的岗位
    var IsNurse = '@ViewBag.IsNurse';
    if (IsVitalSignsSwitch == 'ON' && IsNurse == "True") {
        $("#myTab li:not(#librlb)").css('display', 'none');
        $("#radioTreating,#radioTreated").addClass('newtouch_Readonly').attr('disabled', 'disabled');
        $("#btn_startTreat,#btn_resumeTreat").hide();
    }

    $(function () {
        //show：是否呈现tab内容的事件
        $('a[data-toggle="tab"]').on('show.bs.tab', function (e) {
            var targetHref = $(e.target).attr('href');    //e.target  激活的标签页
            if (targetHref && targetHref.indexOf('linkbrlb') == -1) {
                //非患者列表页
                if (!!!window.currPatientInfo || JSON.stringify(window.currPatientInfo) == '{}') {
                    $.modalAlert("请先选择一个患者", 'warning');
                    return false;
                }
                if (window.currPatientInfo && window.currPatientInfo.jzzt == '@((int)EnumJzzt.Treated)'
                    && targetHref.indexOf('linkbl') == -1) {
                    $.modalAlert("已结束就诊仅可查看", 'warning');
                    return false;
                }
            }
            var relatedTargetHref = $(e.relatedTarget).attr('href');
            if (relatedTargetHref) {
                if (relatedTargetHref.indexOf('linkkfcf') != -1 && $("#gridkfcf:visible").length == 1) {
                    if ($("#gridkfcf").jqGrid('getDataIDs').length > 0) {
                        //先保存
                        SaveRehabPres(null, true);
                    } else {
                        //window.alldataArray.kfcf = [];
                    }
                }
                else if (relatedTargetHref.indexOf('linkcgxmcf') != -1 && $("#gridcgxmcf:visible").length == 1) {
                    if ($("#gridcgxmcf").jqGrid('getDataIDs').length > 0) {
                        //先保存
                        SaveRegularItemPres(null, true);
                    } else {
                        //window.alldataArray.cgxmcf = [];
                    }
                }
                else if (relatedTargetHref.indexOf('linkxycf') != -1 && $("#gridxycf:visible").length == 1) {
                    if ($("#gridxycf").jqGrid('getDataIDs').length > 0) {
                        //先保存
                        SaveWMPres(null, true);
                    } else {
                        //window.alldataArray.xycf = [];
                    }
                }
                else if (relatedTargetHref.indexOf('linkzycf') != -1 && $("#gridzycf:visible").length == 1) {
                    if ($("#gridzycf").jqGrid('getDataIDs').length > 0) {
                        //先保存
                        SaveTCMPres(null, true);
                    } else {
                        //window.alldataArray.zycf = [];
                    }
                }
                else if (relatedTargetHref.indexOf('linkjy') != -1 && $("#alreadyJYAppliedList:visible").length == 1) {
                    if ($("#alreadyJYAppliedList").jqGrid('getDataIDs').length > 0) {
                        //先保存
                        SaveJY(null, true);
                    } else {
                        //window.alldataArray.jccf = [];
                    }
                }
                else if (relatedTargetHref.indexOf('linkjc') != -1 && $("#alreadyJCAppliedList:visible").length == 1) {
                    if ($("#alreadyJCAppliedList").jqGrid('getDataIDs').length > 0) {
                        //先保存
                        SaveJC(null, true);
                    } else {
                       // window.alldataArray.jycf = [];
                    }
                }
                
            }
        })

        //shown：tab内容呈现后的事件
        $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
            cfLeftFloatHide();

            var targetHref = $(e.target).attr('href');
            if (targetHref.indexOf('linkkfcf') == -1 && targetHref.indexOf('linkcgxmcf') == -1 && targetHref.indexOf('linkxycf') == -1 && targetHref.indexOf('linkzycf') == -1) {     //非处方页
                $('#floatLeftMostPartDiv').hide();
            }
            else {
                $('#floatLeftMostPartDiv').show();   //显示处方历史模板树
            }
            if (targetHref.indexOf('linkjy') == -1 && targetHref.indexOf('linkjc') == -1) {     //检验检查页
                $('#floatInspectTemplateTree').hide();
            } else {
                $('#floatInspectTemplateTree').show();   //显示检验检查模板树
            }
            switch ($(this).attr('href')) {
                case "#linkbrlb":
                    init_PatientList();
                    break;
                case "#linkbl":
                    init_MedicalRecord();
                    break;
                case "#linkxycf":
                    init_WMPrescription();
                    break;
                case "#linkkfcf":
                    init_RehabPrescription();
                    break;
                case "#linkcgxmcf":
                    init_RegularItemPrescription();
                    break;
                case "#linkzycf":
                    init_TCMPrescription();
                    break;
                case "#linkjy":
                    window.SetCurrentPageJyjclx(@((int)EnumjyjcmbLx.jy));
                    init_Inspection();
                    break;
                case "#linkjc":
                    window.SetCurrentPageJyjclx(@((int)EnumjyjcmbLx.jc));
                    init_Examination();
                    break;
                case "#linkbespeakRegister":
                    init_bespeakRegister();
                default:
                    break;
            }
        })

        $('#myTab a:first').trigger('click'); //初始化显示哪个tab

    });

    //获取新的处方号
    function GetNewPresNo() {
        var newPresNo;
        $.najax({
            url: "/Prescription/GetNewPresNo",
            dataType: "text",
            async: false,
            success: function (data) {
                newPresNo = data;
            }
        });
        return newPresNo;
    }

    var cfColorArr = new Array();
    var cfzhColorArr = new Array();
    var cfSourceColorArr = [
        { color: '#02a0ea', zhcolor: ['#015f95', '#017bbc', '#009ee7', '#4cb7e3', '#95d5f1'] },
        { color: '#27c24c', zhcolor: ['#006c28', '#0b9b2c', '#27c24c', '#6fc874', '#94d794'] },
        { color: '#f05050', zhcolor: ['#a50001', '#d32323', '#f0514e', '#ef827d', '#f5b0a9'] },
        { color: '#1bd0c7', zhcolor: ['#02625e', '#01aca3', '#31d7d5', '#85d5d4', '#c0d2d2'] },
        { color: '#6037af', zhcolor: ['#310075', '#6137b3', '#7266ba', '#938ece', '#b2a9d8'] },
        { color: '#fe7e00', zhcolor: ['#c44300', '#eb4e00', '#ff7d00', '#f89e00', '#f6c579'] },
        { color: '#b100ab', zhcolor: ['#59014f', '#7d197b', '#b13990', '#cc7eb2', '#cda7c0'] },
        { color: '#f50090', zhcolor: ['#910159', '#c10071', '#f40191', '#f462b7', '#faa2d5'] },
        { color: '#6b821a', zhcolor: ['#575c00', '#6b811c', '#91b225', '#98ac4d', '#beca8a'] },
        { color: '#2d61d9', zhcolor: ['#17194c', '#002c8b', '#2d61db', '#6a8ee4', '#97b8e5'] }
    ];

    //获取处方呈现颜色
    function getCfColor(cflx, cfh) {
        var matched = $.jsonWhere(cfColorArr, function (v) {
            return v && v.cflx === cflx && v.cfh === cfh;
        });
        if (matched && matched.length) {
            return matched[0].color;
        }
        //cflx cfh尚未指定颜色

        //这个cflx当前已对应的颜色
        var cflxmatched = $.jsonWhere(cfColorArr, function (v) {
            return v && v.cflx === cflx
        });
        var unusedColor = $.jsonWhere(cfSourceColorArr, function (v) {
            if (v) {
                var usedArr = $.jsonWhere(cflxmatched, function (q) {
                    return q && q.color === v.color;
                });
                if (!usedArr || usedArr.length == 0) {
                    return true;
                }
            }
            return false;
        });
        if (unusedColor && unusedColor.length) {
            var thisColor = unusedColor[0].color;
            cfColorArr.push({ cflx: cflx, cfh: cfh, color: thisColor });
            return thisColor;
        }
        return '#ddd';  //颜色用完了 默认一个颜色
    }

    //释放占用
    function releaseCfColor(cflx, cfh) {
        cfColorArr = $.jsonWhere(cfColorArr, function (v) {
            return !(v && v.cflx === cflx && v.cfh === cfh);
        });
        cfzhColorArr = $.jsonWhere(cfzhColorArr, function (v) {
            return !(v && v.cflx === cflx && v.cfh === cfh && v.zh === zh);
        });
    }

    //获取处方组号呈现颜色
    function getCfZhColor(cflx, cfh, zh) {
        var cfcolor = getCfColor(cflx, cfh);
        var matched = $.jsonWhere(cfSourceColorArr, function (v) {
            return v && v.color === cfcolor;
        });
        if (!matched || matched.length == 0) {
            return '#ddd';  //未指定 默认一个颜色
        }
        var cfzhuseable = matched[0].zhcolor; //组号颜色可选范围
        //开始了
        var matched = $.jsonWhere(cfzhColorArr, function (v) {
            return v && v.cflx === cflx && v.cfh === cfh && v.zh === zh;
        });
        if (matched && matched.length) {
            return matched[0].color;
        }
        //cflx cfh zh尚未指定颜色

        //这个cflx cfh当前已对应的颜色
        var cflxmatched = $.jsonWhere(cfzhColorArr, function (v) {
            return v && v.cflx === cflx && v.cfh === cfh
        });
        var unusedColor = $.jsonWhere(cfzhuseable, function (v) {
            if (v) {
                var usedArr = $.jsonWhere(cflxmatched, function (q) {
                    return q && q.color === v;
                });
                if (!usedArr || usedArr.length == 0) {
                    return true;
                }
            }
            return false;
        });
        if (unusedColor && unusedColor.length) {
            var thisColor = unusedColor[0];
            cfzhColorArr.push({ cflx: cflx, cfh: cfh, zh: zh, color: thisColor });
            return thisColor;
        }
        return '#ddd';  //颜色用完了 默认一个颜色
    }

    //释放占用
    function releaseCfZhColor(cflx, cfh, zh) {
        cfzhColorArr = $.jsonWhere(cfzhColorArr, function (v) {
            return !(v && v.cflx === cflx && v.cfh === cfh && v.zh === zh);
        });
    }

    function showPatientBasicInfo() {
        if (!$('#divPatientBasicInfo').is(':visible')) {
            $('#myTab').css('width', '70%');
            $('#divPatientBasicInfo').show();
        }
        $('#divPatientBasicInfo').html(window.currPatientInfo.xm
         + '&nbsp;/&nbsp;' + window.currPatientInfo.xb
         + '&nbsp;/&nbsp;' + GetAgeByCsny(window.currPatientInfo.csny)
            + '&nbsp;/&nbsp;' + window.currPatientInfo.brxzmc
            + '&nbsp;/&nbsp;<span title=' + window.currPatientInfo.province + "省" + window.currPatientInfo.city + "市" + window.currPatientInfo.county + "县"+window.currPatientInfo.address+'>' + window.currPatientInfo.city + "市"
            + window.currPatientInfo.county + "县</span>");
    }

    function removePatientBasicInfo() {
        if ($('#divPatientBasicInfo').is(':visible')) {
            $('#myTab').css('width', '99%');
            $('#divPatientBasicInfo').hide();
        }
        $('#divPatientBasicInfo').html('');
    }

    function checkItemActivity($li, $thisinput) {
        var selected = false;//默认没有选中当前值
        var selectval = $thisinput.val();
        if (selectval) {
            selectval = selectval.split(',');
            $.each(selectval, function () {
                if (this == $li.html()) {
                    selected = true;
                }
            });
        }
        return selected;
    }
</script>
<script type="text/javascript">
    var ypkcArr = new Array();
    function ypkcrem(yfbmCode, ypCode, sl) {
        for (var iIndex = 0; iIndex < ypkcArr.length; iIndex++) {
            if (ypkcArr[iIndex].yfbmCode == yfbmCode
                && ypkcArr[iIndex].ypCode == ypCode) {
                ypkcArr[iIndex].sl = sl;
                return;
            }
        }
        ypkcArr.push({ yfbmCode: yfbmCode, ypCode: ypCode, sl: sl });
    }
    //库存检验
    function ypkccheck(yfbmCode, ypCode, sl) {
        for (var iIndex = 0; iIndex < ypkcArr.length; iIndex++) {
            if (ypkcArr[iIndex].yfbmCode == yfbmCode
                && ypkcArr[iIndex].ypCode == ypCode) {
                if (ypkcArr[iIndex].sl < sl) {
                    return false;   //库存不足
                }
                return true;
            }
        }
        return true;
    }

    function cfLeftFloatHide() {
        $('#floadHistoryPresTree').hide();
        $('#floatPreTemplateTree').hide();
        $("#floatLeftMostPartDiv div").removeClass("activepart");
    }

    $('.contentPanel').on('click', function () {
        cfLeftFloatHide();
    });
</script>
<script type="text/javascript">
    //弹框修改患者基本信息
    function PatientBasicInfoModify(mzh) {
        $.ajax({
            url: '/Login/GetUALoginAddres',
            data: { appId: "Sett" },
            type: "post",
            async: false,
            dataType: "json",
            success: function (ajaxResp) {
                if (ajaxResp && ajaxResp.data) {
                    var url = ajaxResp.data;
                    var returnUrl = "/PatientManage/HospiterRes/PatientBasic?mzh=" + mzh + "&parent=cisframe",
                    url = url + "&returnUrl=" + encodeURIComponent(returnUrl);
                    $.modalOpen({
                        id: "PatientBasicInfoModify",
                        title: "基本信息修改",
                        url: url,
                        width: "1000px",
                        height: "400px",
                        callBack: function (iframeId) {
                            top.frames[iframeId].submitForm();
                        }
                    });
                }
            }
        });   
    }
</script>